﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region RPTSiteOpeningRecordsDao

        public List<tb_RPT_SiteOpeningRecord> GetAllSiteOpeningRecords()
        {
            var query = (from obj in this.tb_RPT_SiteOpeningRecords
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_SiteOpeningRecord> GetSiteOpeningRecordsByInputDate(DateTime inputdate)
        {

            var query = (from obj in this.tb_RPT_SiteOpeningRecords
                         where obj.IsDelete == 0 && obj.InputDate == inputdate 
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_SiteOpeningRecord> GetSiteOpeningRecordsByInputDate(DateTime inputdate1, DateTime inputdate2)
        {
            var query = (from obj in this.tb_RPT_SiteOpeningRecords
                         where obj.IsDelete == 0 && obj.InputDate >= inputdate1 && obj.InputDate <= inputdate2
                         select obj).ToList();
            return query;
        }

        public List<tb_RPT_SiteOpeningRecord> GetSiteOpeningRecordsByContition(string strWeather, string strInputDate, string strProjectName
                                                                    , string strProjectUnit, string strConstructionSite
                                                                    , string strStrengthGrade, string strDesignSlump
                                                                    , string strFactorySlump, string strCement, string strOpeningTime
                                                                    , string strReserveVolume, string strUsingCement, string strProductionLine
                                                                    , string strApproachTo, string strCarryOn, string strCreateBy)
        {
            var tq = from obj in this.tb_RPT_SiteOpeningRecords
                     where obj.IsDelete == 0
                     select obj;

            if (strWeather.Length > 0)
            {
                tq = tq.Where(entity => entity.Weather == strWeather);
            }

            if (strProjectName.Length > 0)
            {
                tq = tq.Where(entity => entity.ProjectName == strProjectName);
            }
            if (strInputDate.Length > 0)
            {
                tq = tq.Where(entity => entity.InputDate == DateTime.Parse(  strInputDate));
            }
            if (strProjectUnit.Length > 0)
            {
                tq = tq.Where(entity => entity.ProjectUnit == strProjectUnit);
            }
            if (strConstructionSite.Length > 0)
            {
                tq = tq.Where(entity => entity.ConstructionSite == strConstructionSite);
            }
            if (strStrengthGrade.Length > 0)
            {
                tq = tq.Where(entity => entity.StrengthGrade == strStrengthGrade);
            }
            if (strDesignSlump.Length > 0)
            {
                tq = tq.Where(entity => entity.DesignSlump == strDesignSlump);
            }
            if (strFactorySlump.Length > 0)
            {
                tq = tq.Where(entity => entity.FactorySlump == strFactorySlump);
            }
            if (strOpeningTime.Length > 0)
            {
                tq = tq.Where(entity => entity.OpeningTime == DateTime.Parse( strOpeningTime));
            }
            if (strReserveVolume.Length > 0)
            {
                tq = tq.Where(entity => entity.ReserveVolume == strReserveVolume);
            }

            if (strUsingCement.Length > 0)
            {
                tq = tq.Where(entity => entity.UsingCement == strUsingCement);
            }

            if (strProductionLine.Length > 0)
            {
                tq = tq.Where(entity => entity.ProductionLine == strProductionLine);
            } 
             
            if (strApproachTo.Length > 0)
            {
                tq = tq.Where(entity => entity.ApproachTo == strApproachTo);
            }
            if (strCarryOn.Length > 0)
            {
                tq = tq.Where(entity => entity.CarryOn == strCarryOn);
            }

            if (strCreateBy.Length > 0)
            {
                tq = tq.Where(entity => entity.CreateBy == Int32.Parse( strCreateBy));
            }

            var query = tq.ToList();
            return query;
        }

        public tb_RPT_SiteOpeningRecord GetSiteOpeningRecordsById(int id)
        {
            return this.tb_RPT_SiteOpeningRecords.SingleOrDefault(tb_RPT_SiteOpeningRecords => tb_RPT_SiteOpeningRecords.Id == id);
        }

        public void CreateSiteOpeningRecords(tb_RPT_SiteOpeningRecord entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_RPT_SiteOpeningRecords.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifySiteOpeningRecords(tb_RPT_SiteOpeningRecord entity)
        {
            tb_RPT_SiteOpeningRecord instance = this.tb_RPT_SiteOpeningRecords.SingleOrDefault(p => p.Id == entity.Id);
            instance.Weather = entity.Weather;
            instance.InputDate = entity.InputDate;
            instance.ProjectName = entity.ProjectName;
            instance.ProjectUnit = entity.ProjectUnit;
            instance.ConstructionSite = entity.ConstructionSite;
            instance.StrengthGrade = entity.StrengthGrade;
            instance.DesignSlump = entity.DesignSlump;
            instance.FactorySlump = entity.FactorySlump;
            instance.OpeningTime = entity.OpeningTime;
            instance.ReserveVolume = entity.ReserveVolume;
            instance.UsingCement = entity.UsingCement;
            instance.ProductionLine = entity.ProductionLine;
            instance.ApproachTo = entity.ApproachTo;
            instance.CarryOn = entity.CarryOn; 

            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;

            this.SubmitChanges();
        }

        public void DeleteSiteOpeningRecords(tb_RPT_SiteOpeningRecord entity)
        {
            tb_RPT_SiteOpeningRecord deleteEntity = tb_RPT_SiteOpeningRecords.SingleOrDefault(p => p.Id == entity.Id);
            tb_RPT_SiteOpeningRecords.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges();
        }

        #endregion

    }
}
